package com.lwy.qiuchao.safety.controller;

import com.lwy.common.utils.R;
import com.lwy.qiuchao.safety.service.SafetyBoxService;
import com.lwy.qiuchao.safety.service.SafetyCabinetService;
import com.lwy.qiuchao.safety.vo.CabinetInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class SafetyUserController {

    @Autowired
    SafetyCabinetService safetyCabinetService;

    @Autowired
    SafetyBoxService safetyBoxService;

    /**
     * 对特定安全柜信息查询
     *
     * @param fieldId 球场Id
     * @param scId    安全柜Id
     * @return
     */
    @GetMapping("info/{fieldId}/{scId}")
    public R getCabinetInfo(@PathVariable("fieldId") Long fieldId, @PathVariable("scId") Long scId) {

        CabinetInfoVo cabinetInfoVo = safetyCabinetService.getCabinetInfo(fieldId, scId);

        return R.ok().setData(cabinetInfoVo);
    }

    /**
     *  锁定某个箱子
     * @param fieldId
     * @param scId
     * @param mId
     * @param sbId
     * @return
     */
    @PostMapping("lock")
    public R lock(@RequestParam("fieldId") Long fieldId, @RequestParam("scId") Long scId, @RequestParam("mId") Long mId,@RequestParam("sbId") Long sbId) {

        safetyBoxService.lock(fieldId,scId,mId,sbId);

       return R.ok();
    }

    /** 安全柜发出的http请求 对箱门进行解锁
     * @param fieldId
     * @param scId
     * @param pwd 密码：手机号+箱号
     * @param sbId
     * @return
     */
    @PostMapping("unLock")
    public R unlock(@RequestParam("fieldId") Long fieldId, @RequestParam("scId") Long scId, @RequestParam("pwd") String pwd,@RequestParam("sbId") Long sbId){

        Integer integer = safetyBoxService.unLock(fieldId, scId, pwd, sbId);

        if (integer == 1){
            return R.ok("解锁成功");
        }else {
            return R.error(500,"密码错误");
        }
    }

    /**
     * 结束使用安全箱
     * @param mId
     * @return
     */
    @GetMapping("finish/{mId}")
    public R finish(@PathVariable("mId") Long mId){

        safetyBoxService.finish(mId);

        return R.ok("使用结束，请关好箱门");
    }


}